package com.xb.p31_internalstorage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.service.autofill.FillEventHistory;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {

    Button btn_sub,btn_show;
    EditText et_username,et_password;
    TextView tv_show;

    String fileName="File_test";
    String fileName_ext="File_test_ext.txt";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_sub=findViewById(R.id.btn_sub);
        btn_show=findViewById(R.id.btn_show);
        et_username=findViewById(R.id.username);
        et_password=findViewById(R.id.password);
        tv_show=findViewById(R.id.tv_show);

    }

    public void subClick(View view) throws IOException {
        String name=et_username.getText().toString();
        String pwd=et_password.getText().toString();

        FileOutputStream ops=openFileOutput(fileName, Context.MODE_PRIVATE);
        ops.write((name+"##"+pwd).getBytes());
        if(ops!=null){
            ops.close();
        }

        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();


    }

    public void showClick(View view) throws IOException {
        FileInputStream ips=openFileInput(fileName);
        byte[] input = new byte[ips.available()];

        while(ips.read(input)!=-1){
            String str=new String(input);

            String [] strings=str.split("##");
            tv_show.setText("用户名："+strings[0]+";密码："+strings[1]);
        }

        if (ips!=null){
            ips.close();
        }
    }


    public void subClick2(View view) throws IOException {
        String name=et_username.getText().toString();
        String pwd=et_password.getText().toString();

        //1.判断外部存储设备的状态
        checkExternalStorage();
        if (mExtStorage_writeable){
            OutputStream out=new FileOutputStream(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),fileName));
            out.write((name+"##"+pwd).getBytes());
            if(out!=null){
                out.close();
            }
        }
        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
    }

    public void showClick2(View view) throws IOException {
        if (mExtStorage_available){
            File file=new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),fileName);
            if(file.exists()){
                FileInputStream ips=new FileInputStream(file);
                byte[] input = new byte[ips.available()];
                while(ips.read(input)!=-1){
                    String str=new String(input);

                    String [] strings=str.split("##");
                    tv_show.setText("用户名："+strings[0]+";密码："+strings[1]);
                }

                if (ips!=null){
                    ips.close();
                }
            }
        }
        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
    }

    boolean mExtStorage_available=false;//外部设备可用，可读可写
    boolean mExtStorage_writeable=false;//外部设备可写

    public void checkExternalStorage(){
        String state=Environment.getExternalStorageState();
        if(Environment.MEDIA_MOUNTED.equals(state)){//外部设备可用
            mExtStorage_available=true;
            mExtStorage_writeable=true;
        }else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){//可读不可写
            mExtStorage_available=true;
            mExtStorage_writeable=false;
        }else {
            mExtStorage_available=false;
            mExtStorage_writeable=false;
        }
    }
}